import {
	getRegistration,
	validatorProjectNameByUpdate,
	updateRegistration
} from "@/views/wy-asset-management/registration/api/registration.js";
import moment from "moment";
export default {
	data() {
		/**
		 * async / await 同步
		 * 校验项目名称的唯一性
		 */
		let validatorProjectName = async (rule, value, callback) => {
			if (value == "") {
				callback(new Error("请输入项目名称"));
			}
			await validatorProjectNameByUpdate(
				value,
				this.formObj.assetRegistrationId
			).then(respData => {
				let result = respData.data.data;
				if (result == false) {
					callback(new Error("项目名称重复"));
				}
			});
		};

		return {
			loading: true,
			//上传文件相关
			modelType: "asset-registration",
			//弹出框 初始时隐藏
			dialogVisible: false,
			formObj: {
				//基本信息
				assetRegistrationId: this.$route.query.assetRegistrationId,
				projectName: "",
				registrationUnit: "",
				registerDate: "",
				constructionUnit: "",
				constructionAddress: "",
				approvalDocumentNo: "",
				constructionStartTime: "",
				completedTime: "",
				finalAccountsDocumentNo: "",
				assetOccupyingUnit: "",
				superiorCompetentUnit: "",
				assetUseUnit: "",
				fixedAssets: 0,
				currentAssets: 0,
				intangibleAssets: 0,
				deferredAssets: 0,
				otherAssets: 0,
				stayWriteOffExpenditure: 0,
				total: 0,
				remarks: "",
				filledBy: "",
				reviewer: "",
				personInCharge: "",
				//编辑历史记录
				assetRegistrationLogList: []
			},
			fileList: [],
			rules: {
				projectName: [{ validator: validatorProjectName, trigger: "blur" }],
				registrationUnit: [
					{
						required: true,
						message: "请输入登记单位",
						trigger: "blur"
					}
				],
				registerDate: [
					{
						required: true,
						message: "请输入填表日期",
						trigger: "blur"
					}
				],
				constructionUnit: [
					{
						required: true,
						message: "请输入建设单位",
						trigger: "blur"
					}
				],
				constructionAddress: [
					{
						required: true,
						message: "请输入项目建设地址",
						trigger: "blur"
					}
				],
				approvalDocumentNo: [
					{
						required: true,
						message: "请输入设计立项文号",
						trigger: "blur"
					}
				],
				constructionStartTime: [
					{
						required: true,
						message: "请输入建设开始时间",
						trigger: "blur"
					}
				],
				completedTime: [
					{
						required: true,
						message: "请输入竣工时间",
						trigger: "blur"
					},
					{ validator: this.validateCompletionDate, trigger: "blur" }
				],
				finalAccountsDocumentNo: [
					{
						required: true,
						message: "请输入决算批复文号",
						trigger: "blur"
					}
				],
				assetOccupyingUnit: [
					{
						required: true,
						message: "请输入资产占用单位",
						trigger: "blur"
					}
				],
				superiorCompetentUnit: [
					{
						required: true,
						message: "请输入上级主管单位",
						trigger: "blur"
					}
				],
				assetUseUnit: [
					{
						required: true,
						message: "请输入资产使用单位",
						trigger: "blur"
					}
				],
				filledBy: [
					{ required: true, message: "请输入填报人", trigger: "blur" }
				],
				reviewer: [
					{ required: true, message: "请输入复核人", trigger: "blur" }
				],
				personInCharge: [
					{
						required: true,
						message: "请输入单位负责人",
						trigger: "blur"
					}
				],
				//金额校验
				fixedAssets: [
					{
						required: true,
						message: "请输入金额",
						trigger: "change"
					},
					{
						pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
						message: "请输入合法的金额数字，最多两位小数",
						trigger: "change"
					}
				],
				currentAssets: [
					{
						required: true,
						message: "请输入金额",
						trigger: "change"
					},
					{
						pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
						message: "请输入合法的金额数字，最多两位小数",
						trigger: "change"
					}
				],
				intangibleAssets: [
					{
						required: true,
						message: "请输入金额",
						trigger: "change"
					},
					{
						pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
						message: "请输入合法的金额数字，最多两位小数",
						trigger: "change"
					}
				],
				deferredAssets: [
					{
						required: true,
						message: "请输入金额",
						trigger: "change"
					},
					{
						pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
						message: "请输入合法的金额数字，最多两位小数",
						trigger: "change"
					}
				],
				otherAssets: [
					{
						required: true,
						message: "请输入金额",
						trigger: "change"
					},
					{
						pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
						message: "请输入合法的金额数字，最多两位小数",
						trigger: "change"
					}
				],
				stayWriteOffExpenditure: [
					{
						required: true,
						message: "请输入金额",
						trigger: "change"
					},
					{
						pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
						message: "请输入合法的金额数字，最多两位小数",
						trigger: "change"
					}
				],
				total: [
					{
						required: true,
						message: "请输入金额",
						trigger: "change"
					},
					{
						pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
						message: "请输入合法的金额数字，最多两位小数",
						trigger: "change"
					}
				]
			}
		};
	},
	created() {
		this.loading = true;
		//计算合计金额
		this.formObj.total =
			parseFloat(this.formObj.fixedAssets) +
			parseFloat(this.formObj.currentAssets) +
			parseFloat(this.formObj.intangibleAssets) +
			parseFloat(this.formObj.deferredAssets) +
			parseFloat(this.formObj.otherAssets) +
			parseFloat(this.formObj.stayWriteOffExpenditure);

		//获取数据
		getRegistration(this.$route.query.assetRegistrationId)
			.then(resp => {
				let obj = resp.data.data;
				this.formObj = obj;
			})
			.catch(error => {
				this.$message({
					type: "error",
					message: "获取登记信息失败"
				});
			})
			.finally(() => (this.loading = false));
	},
	methods: {
		// 竣工日期校验
		validateCompletionDate(rule, value, callback) {
			if (!this.formObj.constructionStartTime) return callback();
			const startDate = moment(value, "YYYY-MM-DD HH:mm:ss");
			const endDate = moment(
				this.formObj.constructionStartTime,
				"YYYY-MM-DD HH:mm:ss"
			);
			let diffDay = startDate.diff(endDate, "seconds");
			if (diffDay < 1) {
				return callback(new Error("竣工日期必须大于建设开始日期"));
			}
			return callback();
		},
		/**
		 * 计算合计金额
		 */
		calculationTotal() {
			this.formObj.total =
				parseFloat(this.formObj.fixedAssets) +
				parseFloat(this.formObj.currentAssets) +
				parseFloat(this.formObj.intangibleAssets) +
				parseFloat(this.formObj.deferredAssets) +
				parseFloat(this.formObj.otherAssets) +
				parseFloat(this.formObj.stayWriteOffExpenditure);
		},
		/**
		 * 保存表单信息
		 * @returns {Promise<void>}
		 */
		saveEntity() {
			this.$refs["ruleForm"].validate(valid => {
				if (valid) {
					this.fileList.length > 0 &&
						this.$refs.registrationEditFile.submit(
							this.modelType,
							this.formObj.assetRegistrationId
						);
					let formData = this.formObj;
					updateRegistration(formData)
						.then(data => {
							this.$message({
								type: "success",
								message: "操作成功"
							});
							//操作完成 跳转页面
							this.$router.push("/asset-management/registration-list");
						})
						.catch(error => {
							this.$message({
								type: "error",
								message: "操作失败"
							});
						})
						.finally(() => (this.loading = false));
				} else {
					return false;
				}
			});
		}
	}
};
